package Easy.Array;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: white-zhou
 * @date: 2024-09-10 19:29
 **/
public class MajorityElement {
    public int majorityElement(int[] nums) {
        Arrays.sort(nums);
        return nums[nums.length / 2];
    }

    public int hash(int[] nums) {
        int n = nums.length / 2;
        Map<Integer, Integer> map = new HashMap<>();
        for (int num : nums) {
            int count = map.getOrDefault(num, 0) + 1;
            if (count > n) {
                return num;
            }
            map.put(num, count);
        }
        return -1;
    }
}
